﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GenaralLibs;
using Lists;
using System.Data.SqlClient;

namespace Lists
{
    public class HoSoDangKy : DoiTuong
    {
        #region int NamHoSo property
        public int NamHoSo
        {
            get
            {
                if (_DataRow["NamHoSo"] is int) return (int)_DataRow["NamHoSo"];
                return 0;
            }
            set
            {
                _DataRow["NamHoSo"] = value;
            }
        }
        #endregion

        #region int SoTT property
        public int SoTT
        {
            get
            {
                if (_DataRow["SoTT"] is int) return (int)_DataRow["SoTT"];
                return 0;
            }
            set
            {
                _DataRow["SoTT"] = value;
            }
        }
        #endregion

        #region int DiaBanID property
        public int DiaBanID
        {
            get
            {
                if (_DataRow["DiaBanID"] is int) return (int)_DataRow["DiaBanID"];
                return 0;
            }
            set
            {
                _DataRow["DiaBanID"] = value;
            }
        }
        #endregion

        #region string SoHoSo property
        public string SoHoSo
        {
            get
            {
                if (_DataRow["SoHoSo"] is string) return (string)_DataRow["SoHoSo"];
                return "";
            }
            set
            {
                _DataRow["SoHoSo"] = value;
            }
        }
        #endregion

        #region int CanBoCapID property
        public int CanBoCapID
        {
            get
            {
                if (_DataRow["CanBoCapID"] is int) return (int)_DataRow["CanBoCapID"];
                return 0;
            }
            set
            {
                _DataRow["CanBoCapID"] = value;
            }
        }
        #endregion

        #region int CanBoSuaID property
        public int CanBoSuaID
        {
            get
            {
                if (_DataRow["CanBoSuaID"] is int) return (int)_DataRow["CanBoSuaID"];
                return 0;
            }
            set
            {
                _DataRow["CanBoSuaID"] = value;
            }
        }
        #endregion

        #region DateTime ThoiGianSua property
        public DateTime? ThoiGianSua
        {
            get
            {
                if (_DataRow["ThoiGianSua"] is DateTime) return (DateTime)_DataRow["ThoiGianSua"];
                return null;
            }
            set
            {
                _DataRow["ThoiGianSua"] = value;
            }
        }
        #endregion

        #region int LoaiHoSoID property
        public int LoaiHoSoID
        {
            get
            {
                if (_DataRow["LoaiHoSoID"] is int) return (int)_DataRow["LoaiHoSoID"];
                return 0;
            }
            set
            {
                _DataRow["LoaiHoSoID"] = value;
            }
        }
        #endregion

        #region DateTime NgayCap property
        public DateTime? NgayCap
        {
            get
            {
                if (_DataRow["NgayCap"] is DateTime) return (DateTime)_DataRow["NgayCap"];
                return null;
            }
            set
            {
                _DataRow["NgayCap"] = value;
            }
        }
        #endregion

        #region string TenCoSoKD property
        public string TenCoSoKD
        {
            get
            {
                if (_DataRow["TenCoSoKD"] is string) return (string)_DataRow["TenCoSoKD"];
                return "";
            }
            set
            {
                _DataRow["TenCoSoKD"] = value;
            }
        }
        #endregion

        #region string SoGiayPhepKD property
        public string SoGiayPhepKD
        {
            get
            {
                if (_DataRow["SoGiayPhepKD"] is string) return (string)_DataRow["SoGiayPhepKD"];
                return "";
            }
            set
            {
                _DataRow["SoGiayPhepKD"] = value;
            }
        }
        #endregion

        #region DateTime NgayCapPhepKD property
        public DateTime? NgayCapPhepKD
        {
            get
            {
                if (_DataRow["NgayCapPhepKD"] is DateTime) return (DateTime)_DataRow["NgayCapPhepKD"];
                return null;
            }
            set
            {
                _DataRow["NgayCapPhepKD"] = value;
            }
        }
        #endregion

        #region string CoQuanCapKD property
        public string CoQuanCapKD
        {
            get
            {
                if (_DataRow["CoQuanCapKD"] is string) return (string)_DataRow["CoQuanCapKD"];
                return "";
            }
            set
            {
                _DataRow["CoQuanCapKD"] = value;
            }
        }
        #endregion

        #region string DiaDiemKD property
        public string DiaDiemKD
        {
            get
            {
                if (_DataRow["DiaDiemKD"] is string) return (string)_DataRow["DiaDiemKD"];
                return "";
            }
        }
        #endregion

        #region string DiaDiem_KD1 property
        public string DiaDiem_KD1
        {
            get
            {
                if (_DataRow["DiaDiem_KD1"] is string) return (string)_DataRow["DiaDiem_KD1"];
                return "";
            }
            set
            {
                _DataRow["DiaDiem_KD1"] = value;
            }
        }
        #endregion

        #region int DiaDiem_KD2_DiaBanID property
        public int DiaDiem_KD2_DiaBanID
        {
            get
            {
                if (_DataRow["DiaDiem_KD2_DiaBanID"] is int) return (int)_DataRow["DiaDiem_KD2_DiaBanID"];
                return 0;
            }
            set
            {
                _DataRow["DiaDiem_KD2_DiaBanID"] = value;
            }
        }
        #endregion

        #region string ChuCoSo property
        public string ChuCoSo
        {
            get
            {
                if (_DataRow["ChuCoSo"] is string) return (string)_DataRow["ChuCoSo"];
                return "";
            }
            set
            {
                _DataRow["ChuCoSo"] = value;
            }
        }
        #endregion

        #region byte[] AnhDaiDien property
        public byte[] AnhDaiDien
        {
            get
            {
                if (_DataRow["AnhDaiDien"] is byte[]) return (byte[])_DataRow["AnhDaiDien"];
                return new byte[0];
                //return "";
            }
            set
            {
                _DataRow["AnhDaiDien"] = (byte[])value;
            }
        }
        #endregion

        #region bool GioiTinh property
        public bool? GioiTinh
        {
            get
            {
                if (_DataRow["GioiTinh"] is bool) return (bool)_DataRow["GioiTinh"];
                return null;
            }
            set
            {
                _DataRow["GioiTinh"] = value;
            }
        }
        #endregion

        #region DateTime NgaySinh property
        public DateTime? NgaySinh
        {
            get
            {
                if (_DataRow["NgaySinh"] is DateTime) return (DateTime)_DataRow["NgaySinh"];
                return null;
            }
            set
            {
                _DataRow["NgaySinh"] = value;
            }
        }
        #endregion

        #region int NamSinh property
        public int NamSinh
        {
            get
            {
                if (_DataRow["NamSinh"] is int) return (int)_DataRow["NamSinh"];
                return 0;
            }
            set
            {
                _DataRow["NamSinh"] = value;
            }
        }
        #endregion

        #region string SoCMND property
        public string SoCMND
        {
            get
            {
                if (_DataRow["SoCMND"] is string) return (string)_DataRow["SoCMND"];
                return "";
            }
            set
            {
                _DataRow["SoCMND"] = value;
            }
        }
        #endregion

        #region DateTime NgayCapCMND property
        public DateTime? NgayCapCMND
        {
            get
            {
                if (_DataRow["NgayCapCMND"] is DateTime) return (DateTime)_DataRow["NgayCapCMND"];
                return null; 
            }
            set
            {
                _DataRow["NgayCapCMND"] = value;
            }
        }
        #endregion

        #region string DonViCap property
        public string DonViCap
        {
            get
            {
                if (_DataRow["DonViCap"] is string) return (string)_DataRow["DonViCap"];
                return "";
            }
            set
            {
                _DataRow["DonViCap"] = value;
            }
        }
        #endregion

        #region int DanTocID property
        public int DanTocID
        {
            get
            {
                if (_DataRow["DanTocID"] is int) return (int)_DataRow["DanTocID"];
                return 0;
            }
            set
            {
                _DataRow["DanTocID"] = value;
            }
        }
        #endregion

        #region int QuocTichID property
        public int QuocTichID
        {
            get
            {
                if (_DataRow["QuocTichID"] is int) return (int)_DataRow["QuocTichID"];
                return 0;
            }
            set
            {
                _DataRow["QuocTichID"] = value;
            }
        }
        #endregion

        #region string DienThoai property
        public string DienThoai
        {
            get
            {
                if (_DataRow["DienThoai"] is string) return (string)_DataRow["DienThoai"];
                return "";
            }
            set
            {
                _DataRow["DienThoai"] = value;
            }
        }
        #endregion

        #region string HK_ThuongTru property
        public string HK_ThuongTru
        {
            get
            {
                if (_DataRow["HK_ThuongTru"] is string) return (string)_DataRow["HK_ThuongTru"];
                return "";
            }
            set
            {
                _DataRow["HK_ThuongTru"] = value;
            }
        }
        #endregion

        #region string HienO property
        public string HienO
        {
            get
            {
                if (_DataRow["HienO"] is string) return (string)_DataRow["HienO"];
                return "";
            }
            set
            {
                _DataRow["HienO"] = value;
            }
        }
        #endregion

        #region string DiaChi property
        public string DiaChi
        {
            get
            {
                if (_DataRow["DiaChi"] is string) return (string)_DataRow["DiaChi"];
                return "";
            }
            set
            {
                _DataRow["DiaChi"] = value;
            }
        }
        #endregion

        #region int MoHinhID property
        public int MoHinhID
        {
            get
            {
                if (_DataRow["MoHinhID"] is int) return (int)_DataRow["MoHinhID"];
                return 0;
            }
            set
            {
                _DataRow["MoHinhID"] = value;
            }
        }
        #endregion

        #region int NganhNgheID property
        public int NganhNgheID
        {
            get
            {
                if (_DataRow["NganhNgheID"] is int) return (int)_DataRow["NganhNgheID"];
                return 0;
            }
            set
            {
                _DataRow["NganhNgheID"] = value;
            }
        }
        #endregion

        #region int TrangThaiHS property
        public int TrangThaiHS
        {
            get
            {
                if (_DataRow["TrangThaiHS"] is int) return (int)_DataRow["TrangThaiHS"];
                return 0;
            }
            set
            {
                _DataRow["TrangThaiHS"] = value;
            }
        }
        #endregion

        #region DateTime NgayNghiKD property
        public DateTime? NgayNghiKD
        {
            get
            {
                if (_DataRow["NgayNghiKD"] is DateTime) return (DateTime)_DataRow["NgayNghiKD"];
                return null;
            }
            set
            {
                _DataRow["NgayNghiKD"] = value;
            }
        }
        #endregion

        #region int TrangThai property
        public int TrangThai
        {
            get
            {
                if (_DataRow["TrangThai"] is int) return (int)_DataRow["TrangThai"];
                return 0;
            }
            set
            {
                _DataRow["TrangThai"] = value;
            }
        }
        #endregion

        #region string NoiCapPCCC property
        public string NoiCapPCCC
        {
            get
            {
                if (_DataRow["NoiCapPCCC"] is string) return (string)_DataRow["NoiCapPCCC"];
                return "";
            }
            set
            {
                _DataRow["NoiCapPCCC"] = value;
            }
        }
        #endregion

        #region int TongSoCO2 property
        public int TongSoCO2
        {
            get
            {
                if (_DataRow["TongSoCO2"] is int) return (int)_DataRow["TongSoCO2"];
                return 0;
            }
            set
            {
                _DataRow["TongSoCO2"] = value;
            }
        }
        #endregion

        #region int TongSoBinhBot property
        public int TongSoBinhBot
        {
            get
            {
                if (_DataRow["TongSoBinhBot"] is int) return (int)_DataRow["TongSoBinhBot"];
                return 0;
            }
            set
            {
                _DataRow["TongSoBinhBot"] = value;
            }
        }
        #endregion

        #region int TongSoLangVoi property
        public int TongSoLangVoi
        {
            get
            {
                if (_DataRow["TongSoLangVoi"] is int) return (int)_DataRow["TongSoLangVoi"];
                return 0;
            }
            set
            {
                _DataRow["TongSoLangVoi"] = value;
            }
        }
        #endregion

        #region int TongSoMayBom property
        public int TongSoMayBom
        {
            get
            {
                if (_DataRow["TongSoMayBom"] is int) return (int)_DataRow["TongSoMayBom"];
                return 0;
            }
            set
            {
                _DataRow["TongSoMayBom"] = value;
            }
        }
        #endregion

        #region int TongSoNguoi property
        public int TongSoNguoi
        {
            get
            {
                if (_DataRow["TongSoNguoi"] is int) return (int)_DataRow["TongSoNguoi"];
                return 0;
            }
            set
            {
                _DataRow["TongSoNguoi"] = value;
            }
        }
        #endregion

        #region string GhiChu property
        public string GhiChu
        {
            get
            {
                if (_DataRow["GhiChu"] is string) return (string)_DataRow["GhiChu"];
                return "";
            }
            set
            {
                _DataRow["GhiChu"] = value;
            }
        }
        #endregion

        public HoSoDangKy(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucHoSoDangKy : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucHoSoDangKy _Instance;
        public static DanhMucHoSoDangKy Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucHoSoDangKy();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "HoSoDangKy";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new HoSoDangKy((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public HoSoDangKy this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new HoSoDangKy(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public HoSoDangKy NewDoiTuong()
        {
            try
            {
                return new HoSoDangKy(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoDangKy GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new HoSoDangKy(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoDangKy GetByID(int objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucHoSoDangKy GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucHoSoDangKy Filter(string filterExpression)
        {
            try
            {
                DanhMucHoSoDangKy obj = new DanhMucHoSoDangKy();
                //PH
                obj.DataView = new DataView(DanhMucHoSoDangKy.Instance.DataTable, filterExpression, "ID", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucHoSoDangKy GetByTime(DateTime tuNgay, DateTime denNgay)
        {
            return Filter(string.Format("'{0}' < = NgayCap AND NgayCap <= '{1}'", tuNgay, denNgay));
        }

        public DanhMucHoSoDangKy GetByTrangThaiTime(DateTime tuNgay, DateTime denNgay, int maTrangThai)
        {
            string filterExpression = "";
            if (maTrangThai != 3)
                filterExpression = string.Format("'{0}' < = NgayCap AND NgayCap <= '{1}' AND TrangThaiHS = {2}", tuNgay, denNgay, maTrangThai);
            return Filter(filterExpression);
        }
        public DanhMucHoSoDangKy GetByTrangThai(int maTrangThai)
        {
            string filterExpression = "";
            if (maTrangThai != 3)
                filterExpression = string.Format("TrangThaiHS = {0}",maTrangThai);
            return Filter(filterExpression);
        }
        public DanhMucHoSoDangKy GetByTrangString(string strSql)
        {
           return Filter(strSql);
        }
        public void LoadFromDB(string sql)
        {
            SqlConnection con = new SqlConnection(LocalSettings.ConnectString);
            try
            {
                SqlCommand cmd = con.CreateCommand();
                LoadFromDB(cmd, sql);
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
            finally
            {
                con.Close();
            }
        }

        public void LoadFromDB(SqlCommand cmd, string sql)
        {
            try
            {
                DataTable.Clear();
                cmd.CommandText = sql;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(DataTable);
            }
            catch (SqlException sqlEx)
            {
                throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }   
    }
}